<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // https://leetcode-cn.com/problems/first-unique-character-in-a-string/
        // 用 Map 来做
        // 简单题
        // 自己的哈希写的太丑，看看别人的
        {
            var firstUniqChar = function (s) {
                let arr = s.split('');
                let map = new Map();
                for (let i = 0; i < arr.length; i++) {
                    if (map.has(arr[i])) {
                        map.set(arr[i], -1)
                    } else {
                        map.set(arr[i], i)
                    }
                }
                return Array.from(map).filter(item => {
                    return item[1] != -1
                }).length === 0 ? -1 : Array.from(map).filter(item => {
                    return item[1] != -1
                })[0][1]
            };

            console.log(firstUniqChar('leetcode'));
        }

    </script>
    <script>
        {
            var firstUniqChar = function (s) {
                let map = new Map()
                for (let str of s) {
                    // 快速创建记录数频的哈希表
                    map.set(str, map.has(str) ? map.get(str) + 1 : 1);
                }
                for (let i = 0; i < s.length; i++) {
                    if (map.get(s[i]) === 1) {
                        return i
                    }
                }
                return -1
            };
        }
    </script>
</body>

</html>